<?php
	class XmlSource {
		protected $nodes = null;
		protected $attrs = null;
		protected $name  = 'XmlSource';
		protected $content;

		function __construct($name=false,$content=false) {
			if(!$name) $name = 'XmlSource';
			$this->name = $name;
			$this->nodes = array();
			$this->attrs = array();
			$this->content = $content;
		}

		public function Name($newName=false) {
			if($newName) {
				$this->name = $newName;
			}
			return $this->name;
		}
		
		public function Content($newContent=false) {
			if($newContent) {
				$this->content = $newContent;
			}
			return $this->content;
		}		

		public function MountSource($source, $name='') {
			echo $this->content;
        	$item = new stdClass();
        	$item->name		= $name?$name:$source->Name();
        	$item->source	= $source;
			
			$this->nodes[]  = $item;
		}

		public function Attribute($name, $value=null) {
			if(!$value) return $this->attrs[$name];
			$this->attrs[$name] = $value;
		}

		public function AsDom() {
			$doc = new DOMDocument('1.0', 'UTF-8');
			$doc->appendChild($root = new DOMElement($this->name,$this->content));
			$this->ToNode($root);

			$doc->formatOutput = true;
			return $doc;
		}

		public function ToNode($node) {
			foreach($this->attrs as $attr => $val) {
				$node->setAttribute($attr, $val);
			}

			foreach($this->nodes as $item) {
				$node->appendChild($x = new DOMElement($item->name, $item->source->content));
				$item->source->ToNode($x);
			}
		}
	}
?>